package com.yupi.moshu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yupi.moshu.common.BaseResponse;
import com.yupi.moshu.common.ErrorCode;
import com.yupi.moshu.common.ResultUtils;
import com.yupi.moshu.model.domain.Prototype;
import com.yupi.moshu.model.request.*;
import com.yupi.moshu.service.PrototypeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 原型管理接口
 * @author 7
 */
@RestController
@RequestMapping("/prototype")
public class prototypeController {
    @Resource
    private PrototypeService prototypeService;

    @PostMapping(value = "/save")
    public BaseResponse<Void> modifyDoc(@RequestBody SaveProtoRequest saveProtoRequest) {
        Prototype prototype = new Prototype();
        prototype.setProtName(saveProtoRequest.getProtName());
        prototype.setTempDesign(saveProtoRequest.getTempDesign());
        prototype.setProjectID(saveProtoRequest.getProjectID());
        prototypeService.save(prototype);
        return ResultUtils.success("原型保存成功");
    }

    @PostMapping(value = "/load")
    public BaseResponse<Prototype> modifyDoc(@RequestBody LoadProtoRequest loadProtoRequest) {
        Prototype prototype = prototypeService.getById(loadProtoRequest.getProtID());
        return ResultUtils.success(prototype);
    }

    @PostMapping(value = "/list")
    public BaseResponse<List<Prototype>> modifyDoc(@RequestBody ProtoListRequest protoListRequest) {
        QueryWrapper<Prototype> prototypeQueryWrapper = new QueryWrapper<>();
        prototypeQueryWrapper.eq("projectID", protoListRequest.getProjectID());
        List<Prototype> prototypeList = prototypeService.list(prototypeQueryWrapper);
        return ResultUtils.success(prototypeList);
    }

    @PostMapping(value = "/rename")
    public BaseResponse<Void> modifyDoc(@RequestBody RenameProtoRequest renameProtoRequest) {
        UpdateWrapper<Prototype> prototypeUpdateWrapper = new UpdateWrapper<>();
        prototypeUpdateWrapper.eq("id", renameProtoRequest.getProtID());
        prototypeUpdateWrapper.set("protName", renameProtoRequest.getProtName());
        prototypeService.update(prototypeUpdateWrapper);
        return ResultUtils.success("名称已修改");
    }


    @PostMapping(value = "/modify")
    public BaseResponse<Void> modifyDoc(@RequestBody ModifyProtRequest modifyProtRequest) {
        UpdateWrapper<Prototype> prototypeUpdateWrapper = new UpdateWrapper<>();
        prototypeUpdateWrapper.eq("id", modifyProtRequest.getProtID());
        prototypeUpdateWrapper.set("tempDesign", modifyProtRequest.getTempDesign());
        prototypeUpdateWrapper.set("isLocked",0);
        prototypeService.update(prototypeUpdateWrapper);
        return ResultUtils.success("修改已保存");
    }

    @PostMapping(value = "/lock")
    public BaseResponse<Void> lockDoc(@RequestBody LockProtoRequest lockProtoRequest) {

        Prototype prototype = prototypeService.getById(lockProtoRequest.getProtID());
        if (prototype.getIsLocked()!=0){
            return ResultUtils.error(ErrorCode.NO_AUTH, "有他人在修改此原型，请稍等");
        }
        else {
            UpdateWrapper<Prototype> prototypeUpdateWrapper = new UpdateWrapper<>();
            prototypeUpdateWrapper.eq("id", lockProtoRequest.getProtID());
            prototypeUpdateWrapper.set("isLocked",1);
            prototypeService.update(prototypeUpdateWrapper);
            return ResultUtils.success("请开始修改");
        }
    }

}
